package cn.dankal.flyaudio.presenter;

import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.List;

import androidx.annotation.NonNull;
import api.ShopUserServiceFactory;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.pojo.ShopCarEntity;
import cn.dankal.basiclib.pojo.req.AddShopCarReq;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.flyaudio.contract.ShopCarContract;

public class ShopCarPresenter implements ShopCarContract.Presenter {

    private ShopCarContract.View mView;

    @Override
    public void attachView(@NonNull ShopCarContract.View view) {
        mView = view;
    }

    @Override
    public void detachView() {
        mView = null;
    }

    @Override
    public void getMyShopCar() {
        ShopUserServiceFactory.mySHopCar()
                .subscribe(new AbstractSubscriber<BaseResponse<List<ShopCarEntity>>>(mView) {
                    @Override
                    public void onNext(BaseResponse<List<ShopCarEntity>> listBaseResponse) {
                        mView.showShopCarList(listBaseResponse.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.showError();
                    }
                });
    }

    @Override
    public void addProduct(String itemUuid, String count) {
        AddShopCarReq req = new AddShopCarReq();
        req.setItemUuid(itemUuid);
        req.setItemCount(count + "");
        ShopUserServiceFactory.addShopCar(req)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> productEntityBaseResponse) {
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    @Override
    public void deleteProduct(String itemUuid, String count) {
        AddShopCarReq req = new AddShopCarReq();
        req.setItemUuid(itemUuid);
        req.setItemCount(count + "");
        ShopUserServiceFactory.productCount(req)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> productEntityBaseResponse) {
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    @Override
    public void deleteProduct(String uuid) {
        ShopUserServiceFactory.deleteShopCar(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> productEntityBaseResponse) {
                        mView.deleteSuccess();
                        LiveEventBus.get("ShopCarNum",String.class).post("1");
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }


}
